package com.ken.checkviolationapp.ui.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.AdapterView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.ken.base_http_library.Data
import com.ken.base_http_library.Detail
import com.ken.base_http_library.IllegalBean
import com.ken.base_http_library.callback.OnDesCallback
import com.ken.base_library.ui.BaseFragment
import com.ken.base_library.utils.ShowToast
import com.ken.checkviolationapp.R
import com.ken.checkviolationapp.adapter.ViolationsAdapter
import com.ken.checkviolationapp.bean.EmptyData
import com.ken.checkviolationapp.databinding.FragmentHomeBinding
import com.ken.checkviolationapp.utils.SpUtil
import kotlinx.android.synthetic.main.fragment_home.view.*
import kotlinx.android.synthetic.main.include_empty_data.*
import kotlinx.android.synthetic.main.include_empty_data.view.*

class HomeFragment : BaseFragment(), AdapterView.OnItemSelectedListener {
    private val TAG = HomeFragment::class.java.name
    private lateinit var homeViewModel: HomeViewModel
    private var bean = IllegalBean()
    lateinit var adapter: ViolationsAdapter
    private var beanList = arrayListOf<Detail>()
    lateinit var homeBinding: FragmentHomeBinding

    lateinit var rootView: View

    init {
        //        车牌号
        bean.carno = ""
        //        车辆类型
        bean.cartype = "02"
        //        发动机号,支持后6位查询,或全位查询,见注释
        bean.engineno = ""
        //       车架号,支持后6位查询，或全位查询
        bean.frameno = ""
    }

    override fun initDataBind(inflater: LayoutInflater) {
        homeBinding = FragmentHomeBinding.inflate(inflater)
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {

        homeViewModel =
            ViewModelProvider(this, HomeViewModelFactory(bean))[HomeViewModel::class.java]

        homeBinding.handles = this
        setAdapter()
        homeViewModel.mResponse.observe(viewLifecycleOwner, Observer {
            homeBinding.tvOrderNo.text = "订单号:${it.data?.order_no}"
            homeBinding.tvCarNo.text = "车牌号:${it.data?.car_no}"
            homeBinding.tvVioTotal.text = "违章条数:${it.data?.vio_total}"
            homeBinding.tvFindTotal.text = "违章罚款合计:${it.data?.find_total}"
            homeBinding.tvScoreTotal.text = "违章积分合计:${it.data?.score_total}"
            if (it.data?.details?.isEmpty()!!) {
                llEmptyData.visibility = View.VISIBLE
            } else {
                for (item in it.data!!.details) {
                    beanList.add(item)
                }
            }
            adapter.notifyDataSetChanged()
        })
        homeBinding.root.spCartype.setSelection(1)
        homeBinding.etCarno.setText(SpUtil.getString(SpUtil.carNo, ""))
        homeBinding.etEngineno.setText(SpUtil.getString(SpUtil.engineno, ""))
        homeBinding.etFrameno.setText(SpUtil.getString(SpUtil.frameno, ""))
        val empty = EmptyData("恭喜!无违章记录")
        homeBinding.data = empty
    }


    override fun initListener() {
        homeBinding.spCartype.onItemSelectedListener = this
        homeBinding.root.btnQuery.setOnClickListener {
            Log.d(TAG, "查询点击1...")
            bean.carno = homeBinding.etCarno.text.toString()
            bean.engineno = homeBinding.etEngineno.text.toString()
            bean.frameno = homeBinding.etFrameno.text.toString()
            if (bean.carno.isEmpty() || bean.engineno.isEmpty() || bean.frameno.isEmpty() || bean.cartype.isEmpty()) {
                ShowToast.show("请完善车辆信息")
                return@setOnClickListener
            }

            SpUtil.saveValue(SpUtil.carNo, bean.carno)
//                            SpUtil.saveValue(SpUtil.carType, bean.cartype)
            SpUtil.saveValue(SpUtil.engineno, bean.engineno)
            SpUtil.saveValue(SpUtil.frameno, bean.frameno)

            homeBinding.pbLoading.visibility = View.VISIBLE
            homeViewModel.getData(bean, object : OnDesCallback<Data> {
                @SuppressLint("SetTextI18n")
                override fun onSuccess(response: Data) {
                    homeBinding.pbLoading.visibility = View.GONE
                    homeBinding.tvOrderNo.text = "订单号:${response.order_no}"
                    homeBinding.tvCarNo.text = "车牌号:${response.car_no}"
                    homeBinding.tvVioTotal.text = "违章条数:${response.vio_total}"
                    homeBinding.tvFindTotal.text = "违章罚款合计:${response.find_total}"
                    homeBinding.tvScoreTotal.text = "违章积分合计:${response.score_total}"
                    homeBinding.root.llEmptyData.visibility = (if (response.details.isEmpty()) {
                        View.VISIBLE
                    } else {
                        View.GONE
                    })

                    if (response.details.isNotEmpty()) {
                        for (item in response.details) {
                            beanList.add(item)
                        }
                    }
                    adapter.notifyDataSetChanged()

                }

                override fun onError(t: Throwable) {
                    homeBinding.pbLoading.visibility = View.GONE
                    t.message?.let { it1 -> ShowToast.show(it1) }
                }


            })
        }

    }

    override fun initData() {


    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }

    override fun databing(): View {
        return homeBinding.root
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {

    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

        bean.cartype = when (position) {
            0 -> {
                "01"
            }
            1 -> {
                "02"
            }
            2 -> {
                "03"
            }
            3 -> {
                "04"
            }
            4 -> {
                "05"
            }
            5 -> {
                "51"
            }
            6 -> {
                "51"
            }

            else -> {
                "01"
            }
        }

    }

    private fun setAdapter() {
        adapter = context?.let { ViolationsAdapter(it, 0, beanList) }!!
        homeBinding.rvList.layoutManager = LinearLayoutManager(context)
        homeBinding.rvList.adapter = adapter
    }
}
